%% Read Model with the Sign Flipped
% by Jaromir Benes
% 
% In this file, we show that models with negative log variables behave
% exactly the same as models in which we flip the sign of these variables
% (creating thus positive log variables).

%% Clear Workspace

clear;
close all;
clc;
irisrequired 20140813;

%% Load Original Model Object `m2`
%
% The original model object `m2` will be used to parameterize the new model
% object created here.

load read_model.mat m2;

%% Load Model File with Flipped Sign
%
% Create a model object `mflip` based on the model file
% `log_minus_flip_sign.model`. This model file is identical to
% `log_minus.model` except that the variable `B` (net assets) is replaced
% with `mB` (net liabilities) defined as `-B`.

mflip = model('log_minus_flip_sign.model');

%% Assign Parameters from `m2`
%
% Assign parameters from the model object `m2`. With these parameters, the
% model `mflip` will have the same steady state and dynamic properties as
% `m2` except that the variable `mB` will be `-B`.

mflip.alp = m2.alp;
mflip.bet = m2.bet;
mflip.zet = m2.zet;
mflip.Rw = m2.Rw;

%% Find Steady State
%
% Compute steady state. Because `mB` is now known to have a positive sign,
% do not use options `'Unlog='` or `'LogMinus='` as in `read_model`.

mflip.Y = 1;
mflip = sstate(mflip,'Growth=',true,'FixLevel=','Y');
chksstate(mflip);

%% Compare Steady State with Original Model
%
% The two models, `m2` and `mflip`, have identical steady states. The only
% difference is the sign of `mB` in `mflip` and `B` in `m2`. The absolute
% magnitudes of the two variables are though obviously the same.

get(m2,'Sstate')
get(mflip,'Sstate')

%% Compute First-Order Solution

mflip = solve(mflip);

%% Compare First-Order Solution Matrices
%
% The elements in the matrices describing first-order dynamics have the
% same magnitudes in both model objects; they only differ in the signs of
% some elements.
%
% Notable difference is in the vectors `K2` and `Kflip`, i.e. the constant
% vectors in transition equations. Because the steady state of one log
% variable is negative, the vector contains complex numbers. In dynamic
% simulations, the imaginary parts always cancel.

[T2,R2,K2,~,~,~,U2] = sspace(m2);
[Tflip,Rflip,Kflip,Zflip,Hflip,Dflip,Uflip] = sspace(mflip);

T2 %#ok<NOPTS>
Tflip %#ok<NOPTS>

R2 %#ok<NOPTS>
Rflip %#ok<NOPTS>

K2 %#ok<NOPTS>
Kflip %#ok<NOPTS>

U2 %#ok<NOPTS>
Uflip %#ok<NOPTS>

%% Save Model Object for Further Use

save read_model_flip_sign.mat mflip;
